﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;
using System.Data;
namespace GenerateDiagram
{
    public class SqlDatabase : AbstractDatabase
    {

        public override List<AbstractDatabase> GetAllDatabases(string databaseName)
        {
            DataConnection.Instance.ConnectionString = databaseName;
            DbConnection cre = DataConnection.Instance.CreateConnection();
            SqlCommand cm = (SqlCommand)cre.CreateCommand();
            cm.CommandText = "select * from sys.databases where owner_sid <>1";
            SqlDataAdapter adap = new SqlDataAdapter();
            List<AbstractDatabase> kq = new List<AbstractDatabase>();

            adap.SelectCommand = cm;
            DataSet ds = new DataSet();
            cre.Open();
            adap.Fill(ds, "databases");
            cre.Close();
            DataTable myDataTable = ds.Tables["databases"];
            foreach (DataRow myDataRow in myDataTable.Rows)
            {
                SqlDatabase sql = new SqlDatabase { Name = myDataRow["name"].ToString(), NamaspaceRender = myDataRow["name"].ToString() };
                kq.Add(sql);

            }
            return kq;
        }

        public override bool SetTables(string databaseName)
        {
            try
            {
                string commandText = "select name from sysobjects where xtype='U' and name<>'dtproperties'and name<>'sysdiagrams' order by name asc";
                DataTable myDataTable = DataConnection.Instance.ExecuteReader(commandText, databaseName);
                Tables = new List<AbstractTable>();
                foreach (DataRow myDataRow in myDataTable.Rows)
                {
                    Tables.Add(new SqlTable { Name = myDataRow["name"].ToString(), ClassName = myDataRow["name"].ToString(), RenderOption = new RenderOption { CanRender=true} });
                }
                return true;
            }
            catch { return false; }
        }

        public override bool GenerateCode(string folderPath)
        {
            throw new NotImplementedException();
        }
    }
}
